package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.exception.ValidateException;
import org.light.utils.DomainUtil;
import org.light.utils.WriteableUtil;

public class ServerGoGenerator{
	protected String fileName = "Server.go";
	protected String packageToken;
	protected String projectName;
	protected Domain sampleDomain;
	public ServerGoGenerator(Domain sampleDomain){
		super();
		this.projectName = sampleDomain.getProjectName();
		this.packageToken = sampleDomain.getPackageToken();
		this.sampleDomain = sampleDomain;
	}

	public String generateString() throws ValidateException{
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package main"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(2500L,0,"import ("));
		//sList.add(new Statement(3000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.getProjectName(),this.getPackageToken()+"."+this.sampleDomain.getDomainSuffix())+"\""));
		sList.add(new Statement(4000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.getProjectName(),"config")+"\""));
		sList.add(new Statement(5000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.getProjectName(),"database")+"\""));
		sList.add(new Statement(6000L,1,"\""+DomainUtil.packagetokenToGoPackageFolder(this.getProjectName(),"router")+"\""));		

		sList.add(new Statement(9000L,1,"\"log\""));
		sList.add(new Statement(10000L,0,""));
		sList.add(new Statement(11000L,1,"\"github.com/gin-gonic/gin\""));
		sList.add(new Statement(12000L,1,"\"github.com/spf13/viper\""));
		sList.add(new Statement(13000L,0,")"));
		sList.add(new Statement(14000L,0,""));
		sList.add(new Statement(15000L,0,"func main() {"));
		sList.add(new Statement(16000L,1,"if err := config.Init(); err != nil {"));
		sList.add(new Statement(17000L,2,"panic(err)"));
		sList.add(new Statement(18000L,1,"}"));
		sList.add(new Statement(19000L,0,""));
		sList.add(new Statement(20000L,1,"if err := database.Init(); err != nil {"));
		sList.add(new Statement(21000L,2,"panic(err)"));
		sList.add(new Statement(22000L,1,"}"));
		sList.add(new Statement(23000L,0,""));
		sList.add(new Statement(25000L,1,"g := gin.Default()"));
		sList.add(new Statement(26000L,0,""));
		sList.add(new Statement(27000L,1,"// Set gin mode."));
		sList.add(new Statement(28000L,1,"gin.SetMode(viper.GetString(\"runmode\"))"));
		sList.add(new Statement(29000L,0,""));
		sList.add(new Statement(30000L,1,"g.Static(\"/pages\", \"./template/pages\")"));
		sList.add(new Statement(31000L,1,"g.Static(\"/css\", \"./template/css\")"));
		sList.add(new Statement(32000L,1,"g.Static(\"/images\", \"./template/images\")"));
		sList.add(new Statement(33000L,1,"g.Static(\"/js\", \"./template/js\")"));
		sList.add(new Statement(34000L,1,"g.Static(\"/uploadjs\", \"./template/uploadjs\")"));
		sList.add(new Statement(35000L,1,"g.Static(\"/easyui\", \"./template/easyui\")"));
		sList.add(new Statement(36000L,1,"g.Static(\"/echarts\", \"./template/echarts\")"));
		sList.add(new Statement(37000L,1,"g.LoadHTMLFiles(\"./template/pages/index.html\")"));
		sList.add(new Statement(38000L,1,"g.GET(\"/index\", func(c *gin.Context) {"));
		sList.add(new Statement(39000L,2,"c.HTML(200, \"index.html\", nil)"));
		sList.add(new Statement(40000L,1,"})"));
		sList.add(new Statement(41000L,0,""));
		sList.add(new Statement(42000L,1,"// Create the Gin engine."));
		sList.add(new Statement(43000L,1,"//g := gin.New()"));
		sList.add(new Statement(44000L,0,""));
		sList.add(new Statement(45000L,1,"router.InitRouter(g)"));
		sList.add(new Statement(46000L,1,"log.Printf(\"Start to listening the incoming requests on http address: %s\", viper.GetString(\"addr\"))"));
		sList.add(new Statement(47000L,1,"//log.Println(http.ListenAndServe(viper.GetString(\"addr\"), g).Error())"));
		sList.add(new Statement(48000L,1,"if err := g.Run(viper.GetString(\"addr\")); err != nil {"));
		sList.add(new Statement(49000L,2,"log.Fatal(\"ListenAndServe:\", err)"));
		sList.add(new Statement(50000L,1,"}"));
		sList.add(new Statement(51000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getPackageToken() {
		return packageToken;
	}

	public void setPackageToken(String packageToken) {
		this.packageToken = packageToken;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

}
